clear all
tempfile tempsave
set seed ${seed}

do "${scripts}_output.do"
do "${scripts}_sumstat.do"
do "${scripts}_textab.do"

/*Macros*/
local data 			"National Longitudinal Study of Adolescent to Adult Health"
local source 		"Own calculations."
local signif 		"Significance levels: * \(p<0.10\), ** \(p<0.05\), *** \(p<0.01\)."
local error 		"Standard errors (in parentheses) are clustered at the school level."
local standard  	"All right-hand side variables are standardized on the estimation sample ($\mu=0$, $\sigma=1$)."
local controls 		"\emph{Child Controls}:" ///
					"Firstborn dummy, linear birth cohort trend (in months) by gender," ///
					"20 principal components of the full matrix of genetic data." ///
					"\emph{Family Controls}:" ///
					"Age of mother at birth, years of education of both mother and father," ///
					"average potential wages of both mother and father," ///
					"the standard deviation of potential wages of both mother and father, dummies for non-US born mothers and fathers," ///
					"a dummy for Christian religion, state fixed effects."
local controlfct	"\emph{Control Function}: Share white, share single mothers, maternal education (average),"  ///
					"share females, share migrants. All control function variables are calculated"  ///
					"as leave-cohort-out school averages."
local controlfct2	"\emph{Control Function}: Share white, share single mothers, maternal education (average),"  ///
					"share females, share migrants."
local notesize scriptsize

// -----------------------------------------------------
// Table S.1
// -----------------------------------------------------
use "${data}data_estim.dta", replace
local name tabS1

/*Get Estimates*/
local models ts1_1 ts1_2 ts1_3 ts1_4 ts1_5 ts1_6
local coefs f_over1 peer_grade_eng peer_grade_mat
output `models', clist(`coefs') replay

/*Set up Table*/
texdoc init "${tablepath}`name'.tex", replace force
#delimit;
tex \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi};
tex \begin{tabular*}{1\linewidth}{@{\hskip\tabcolsep\extracolsep\fill}l*{6}{c}};
tex \toprule;
tex \thead[l]{{Outcome: \PGS}}
			 &\thead{{(1)}}
			 &\thead{{(2)}}
			 &\thead{{(3)}}
			 &\thead{{(4)}}
			 &\thead{{(5)}}
			 &\thead{{(6)}}\\;
tex \midrule;
tex \thead[l]{\IQuali}				&${c_1_1}&${c_2_1}&${c_3_1}&${c_4_1}&${c_5_1}&${c_6_1}\\;
tex \thead[l]{Peer GPA (English)}	&${c_1_2}&${c_2_2}&${c_3_2}&${c_4_2}&${c_5_2}&${c_6_2}\\;
tex \thead[l]{Peer GPA (Math)}		&${c_1_3}&${c_2_3}&${c_3_3}&${c_4_3}&${c_5_3}&${c_6_3}\\;
tex \midrule;
tex \thead[l]{Child Controls}			&\no&\ye&\no&\ye&\no&\ye\\;
tex \thead[l]{Family Controls}			&\no&\ye&\no&\ye&\no&\ye\\;
tex \thead[l]{Control Function}			&\no&\ye&\no&\ye&\no&\ye\\;
tex \thead[l]{N}						&$ob_1&$ob_2&$ob_3&$ob_4&$ob_5&$ob_6\\;
tex \thead[l]{$\text{R}^2$}				&$rs_1&$rs_2&$rs_3&$rs_4&$rs_5&$rs_6\\;
tex \bottomrule;
tex \end{tabular*};
#delimit cr
texdoc close

local title "Gene-Environment Correlations"
local desc  "This table shows gene-environment correlations between \PGS and various school characteristics." ///
			"Peer GPAs are standardized in grade times state cells and calculated as leave-cohort-out school averages."
#delimit;
textab, title(`title') name(`name') replace
width(1.3) file(tex) notesize(`notesize')
note_paper("`source' `desc' `controls' `controlfct' `standard' `signif' `error'")
data(`data')
inpath(${tablepath}) path_paper(${paper_tables});
#delimit cr

// -----------------------------------------------------
// Table S.2
// -----------------------------------------------------
local name tabS2

use "${data}data_estim.dta", replace

/*Get Estimates*/
local models ///
t3_1 ts2_1 ts2_2
local coefs ///
pgs_edu f_over1 c.pgs_edu#c.f_over1
output `models', clist(`coefs') replay

/*Set up Table*/
texdoc init "${tablepath}`name'.tex", replace force
#delimit;
tex \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi};
tex \begin{tabular*}{1\linewidth}{@{\hskip\tabcolsep\extracolsep\fill}l*{3}{c}};
tex \toprule;
tex \multicolumn{1}{c}{~}&\multicolumn{1}{c}{\thead{Baseline}}&\multicolumn{2}{c}{\thead{Excluding Grade 12 in Wave 1}}\\;
tex \cmidrule(lr){2-2}\cmidrule(lr){3-4};
tex	\thead[l]{{Outcome:}\\{Years of Education}}&
	\thead{{}\\{(1)}}&
	\thead{{Leave-cohort-out}\\{(2)}}&
	\thead{{Leave-all-following-cohorts-out}\\{(3)}}\\;
tex \midrule;
tex \thead[l]{\PGS}						&${c_1_1}&${c_2_1}&${c_3_1}\\;
tex \thead[l]{\IQuali}					&${c_1_2}&${c_2_2}&${c_3_2}\\;
tex \thead[l]{\PGS $\times$ \IQuali}	&${c_1_3}&${c_2_3}&${c_3_3}\\;
tex \midrule;
tex \thead[l]{Child Controls}	&\ye&\ye&\ye\\;
tex \thead[l]{Family Controls}	&\ye&\ye&\ye\\;
tex \thead[l]{Control Function}	&\ye&\ye&\ye\\;
tex \thead[l]{N}						&$ob_1&$ob_2&$ob_3\\;
tex \thead[l]{$\text{R}^2$}				&$rs_1&$rs_2&$rs_3\\;
tex \bottomrule;
tex \end{tabular*};
#delimit cr
texdoc close

local title "Robustness to Specification of Control Function"
local desc  "This table shows the joint association of \PGS and \IQuali" ///
			"with completed years of education using different specifications of the control function." ///
			"In column (2) we calculate school averages for the control function based on the original leave-cohort-out specification." ///
			"In column (3) we calculate school averages for the control function while leaving out all subsequent cohorts" ///
			"that follow a respondent's cohort. Note that such school averages cannot be calculated for students in Grade 12 at Wave 1."
#delimit;
textab, title(`title') name(`name') replace
width(1.3) file(tex) notesize(`notesize')
note_paper("`source' `desc' `controls' `controlfct2' `standard' `signif' `error'")
data(`data')
inpath(${tablepath}) path_paper(${paper_tables});
#delimit cr

// -----------------------------------------------------
// Table S.3
// -----------------------------------------------------
use "${data}data_estim.dta", replace
local name tabS3

/*Get Estimates*/
local models ///
t3_1 ts3_1 ts3_2 ts3_3
local coefs ///
pgs_edu f_over1 c.pgs_edu#c.f_over1

output `models', clist(`coefs') replay

/*Set up Table*/
texdoc init "${tablepath}`name'.tex", replace force
#delimit;
tex \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi};
tex \begin{tabular*}{1\linewidth}{@{\hskip\tabcolsep\extracolsep\fill}l*{5}{c}};
tex \toprule;
tex \multicolumn{1}{c}{~}&\multicolumn{1}{c}{\thead{Baseline}}&\multicolumn{3}{c}{\thead{Alternative Sample Composition}}\\;
tex \cmidrule(lr){2-2}\cmidrule(lr){3-5};
tex	\thead[l]{{~}\\{Outcome:}\\{Years of Education}}&
	\thead{{~}\\{~}\\{(1)}}&
	\thead{{Re-}\\{Weighted}\\{(2)}}&
	\thead{{Excl.}\\{(Potential) Movers}\\{before High School}\\{(3)}}&
	\thead{{Excl.}\\{(Potential) Movers}\\{during High School}\\{(4)}}\\;
tex \midrule;
tex \thead[l]{\PGS}						&${c_1_1}&${c_2_1}&${c_3_1}&${c_4_1}\\;
tex \thead[l]{\IQuali}					&${c_1_2}&${c_2_2}&${c_3_2}&${c_4_2}\\;
tex \thead[l]{\PGS $\times$ \IQuali}	&${c_1_3}&${c_2_3}&${c_3_3}&${c_4_3}\\;
tex \midrule;
tex \thead[l]{Child Controls}	&\ye&\ye&\ye&\ye\\;
tex \thead[l]{Family Controls}	&\ye&\ye&\ye&\ye\\;
tex \thead[l]{Control Function}	&\ye&\ye&\ye&\ye\\;
tex \thead[l]{N}						&$ob_1&$ob_2&$ob_3&$ob_4\\;
tex \thead[l]{$\text{R}^2$}				&$rs_1&$rs_2&$rs_3&$rs_4\\;
tex \bottomrule;
tex \end{tabular*};
#delimit cr
texdoc close

local title "Robustness to Sample Composition"
local desc  "This table shows the joint association of \PGS and \IQuali" ///
			"with completed years of education." ///
			"In column (2) we re-weight our analysis sample to match ACS and CPS" ///
			"with respect to gender composition, educational attainment of parents," ///
			"and the age of mothers at birth---see also Supplementary Table \ref{tab: tabS4}." ///
			"In column (3) we exclude respondents that attended feeder schools in wave 1" ///
			"and for whom we do not have information on subsequent high schools." ///
			"In column (4) we additionally exclude respondents for whom we do not have information" ///
			"on whether they graduated from an Add Health high school."
#delimit;
textab, title(`title') name(`name') replace
width(1.3) file(tex) notesize(`notesize')
note_paper("`source' `desc' `controls' `controlfct' `standard' `signif' `error'")
data(`data')
inpath(${tablepath}) path_paper(${paper_tables});
#delimit cr

// -----------------------------------------------------
// Table S.4
// -----------------------------------------------------
use "${temp}represent.dta", clear
split var, parse(_)
rename var1 group

replace var3="Male" 				if group=="gender" 		& var3=="1"
replace var3="Female" 				if group=="gender" 		& var3=="2"
replace var3="$\leq$~High~School" 	///
			if (group=="educmother" | group=="educfather" | group=="educresp")	& var3=="1"
replace var3="$>$~High~School;~$<$~College~Degree" ///
			if (group=="educmother" | group=="educfather" | group=="educresp")	& var3=="2"
replace var3="$\geq$~College~Degree" ///
			if (group=="educmother" | group=="educfather" | group=="educresp")	& var3=="3"
replace var3="$<$~25~Years" 		if group=="agemum"		& var3=="1"
replace var3="$\geq$~25~Years" 		if group=="agemum"		& var3=="2"
replace var3="$<$~\textdollar50,000" 		if group=="hhincome"	& var3=="1"
replace var3="$\geq$~\textdollar50,000;~$<$~\textdollar100,000" ///
			if group=="hhincome"	& var3=="2"
replace var3="$\geq$~\textdollar100,000" 	if group=="hhincome"	& var3=="3"

/*Construct Table*/
local name tabS4

sumstat var3 pop white sample weighted, name(${tablepath}`name') replace ///
flush_l(var3) flush_c(pop white sample weighted) format_3(pop white sample weighted) ///
headlabel("~" "All" "Non-Hispanic~White" "Unweighted" "Re-Weighted") ///
grouporder(gender educmother educfather agemum hhincome educresp) ///
grouplabel("Gender" "Education~Mother" "Education~Father" "Age~Mother~at~Birth" "Parental~Income" "Education~Respondent")	///
head2label("~" "Population~(Cohorts~1974-1983)" "Analysis~Sample") ///
first(var3) long(no) landscape(no) width(1) grouping(yes)  head2(1 2 2)

local title "Sample Representativeness"
local desc 	"This table shows summary statistics of the core analysis sample" ///
	  	   	"in comparison to other population samples." ///
			"It shows respondents' characteristics for the following samples:" ///
			"(i) the US population from birth cohorts 1974--1983," ///
			"(ii) the Non-Hispanic White US population from birth cohorts 1974--1983," ///
			"(iii) the core estimation sample, and" ///
			"(iv) the core estimation sample re-weighted to match (ii) with respect to" ///
			"\emph{Gender}, \emph{Education Mother}, \emph{Education Father}, and \emph{Age Mother at Birth}." ///
			"Population data on \emph{Gender} and \emph{Education Respondent}" ///
			"from IPUMS ACS 2019 \citep{Ruggles2020}." ///
			"Population data on \emph{Education Mother}, \emph{Education Father}, \emph{Age Mother at Birth}, and \emph{Parental Income}" ///
			"from IPUMS CPS 1994 \citep{Flood2020}."
textab,	title(`title') name(`name') replace ///
width(1) file(tex) notesize(`notesize')  ///
note_paper("`source' `desc'") ///
data("`data', American Community Survey (ACS), Current Population Survey (CPS)") ///
inpath(${tablepath}) path_paper(${paper_tables})


// -----------------------------------------------------
// Table S.5
// -----------------------------------------------------
use "${data}data_estim.dta", replace
local name tabS5

/*Get Estimates*/
local models ///
ts5_1 ts5_2 ts5_3 ts5_4 ts5_5 ts5_6 ts5_7 ts5_8
local coefs ///
teach_master teach_5years teach_new class
output `models', clist(`coefs') replay

foreach m in ts5_2 ts5_4 ts5_6 ts5_8{
	estimates use "${results}`m'"
	local d_b_`m'="`: di string(e(d_b),"%9.3f")'"
	local d_se_`m'="`: di string(e(d_se),"%9.3f")'"
	local d_lb_`m'="`: di string(e(d_lb),"%9.3f")'"
	local d_ub_`m'="`: di string(e(d_ub),"%9.3f")'"
	if e(d_p)<=0.1{
		local star_`m' "\sym{*}"
	}
	if e(d_p)<=0.05{
		local  star_`m' "\sym{**}"
	}
	if e(d_p)<=0.01{
		local  star_`m' "\sym{***}"
	}
	if e(d_p)>0.1{
		local star_`m' "\sym{~}"
	}
}

/*Set up Table*/
cap texdoc init "${tablepath}`name'.tex", replace force
#delimit;
tex \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi};
tex \begin{tabular*}{1\linewidth}{@{\hskip\tabcolsep\extracolsep\fill}l*{8}{c}};
tex \toprule;
tex	\multicolumn{1}{c}{\thead[l]{{Outcome:}\\{Predicted Years of Education}}}&
	\multicolumn{2}{c}{\thead{Teacher w/ MA}}&
	\multicolumn{2}{c}{\thead{Exp. Teacher}}&
	\multicolumn{2}{c}{\thead{New Teacher}}&
	\multicolumn{2}{c}{\thead{Class Size}}\\;
tex \cmidrule(lr){2-3}\cmidrule(lr){4-5}\cmidrule(lr){6-7}\cmidrule(lr){8-9};
tex \thead{{~}}&
	\thead{{(1)}}&
	\thead{{(2)}}&
	\thead{{(3)}}&
	\thead{{(4)}}&
	\thead{{(5)}}&
	\thead{{(6)}}&
	\thead{{(7)}}&
	\thead{{(8)}}\\;
tex \midrule;
tex \thead[l]{}					&${c_1_1}&${c_2_1}&${c_3_2}&${c_4_2}&${c_5_3}&${c_6_3}&${c_7_4}&${c_8_4}\\;
tex \midrule;
tex \thead[l]{Difference in coefficients}	&\multicolumn{2}{c}{\thead[c]{{`d_b_ts5_2'`star_ts5_2'}\\{(`d_se_ts5_2')}\\{[`d_lb_ts5_2', `d_ub_ts5_2']}}}
											&\multicolumn{2}{c}{\thead[c]{{`d_b_ts5_4'`star_ts5_4'}\\{(`d_se_ts5_4')}\\{[`d_lb_ts5_4', `d_ub_ts5_4']}}}
											&\multicolumn{2}{c}{\thead[c]{{`d_b_ts5_6'`star_ts5_6'}\\{(`d_se_ts5_6')}\\{[`d_lb_ts5_6', `d_ub_ts5_6']}}}
											&\multicolumn{2}{c}{\thead[c]{{`d_b_ts5_8'`star_ts5_8'}\\{(`d_se_ts5_8')}\\{[`d_lb_ts5_8', `d_ub_ts5_8']}}}\\;
tex \midrule;
tex \thead[l]{Child Controls}			&\no&\no&\no&\no&\no&\no&\no&\no\\;
tex \thead[l]{Family Controls}			&\no&\no&\no&\no&\no&\no&\no&\no\\;
tex \thead[l]{Control Function}			&\no&\ye&\no&\ye&\no&\ye&\no&\ye\\;
tex \thead[l]{N}						&$ob_1&$ob_2&$ob_3&$ob_4&$ob_5&$ob_6&$ob_7&$ob_8\\;
tex \thead[l]{$\text{R}^2$}				&$rs_1&$rs_2&$rs_3&$rs_4&$rs_5&$rs_6&$rs_7&$rs_8\\;
tex \thead[l]{Outcome Mean}				&$mu_1&$mu_2&$mu_3&$mu_4&$mu_5&$mu_6&$mu_7&$mu_8\\;
tex \thead[l]{Outcome SD}				&$sd_1&$sd_2&$sd_3&$sd_4&$sd_5&$sd_6&$sd_7&$sd_8\\;
tex \bottomrule;
tex \end{tabular*};
#delimit cr
texdoc close

local title "Testing for Selection into Schools (Decomposition)"
local desc  "This table shows the associations of the components of \IQuali" ///
			"with predicted years of education." ///
			"Predicted education is calculated from a regression of" ///
			"completed years of education on all \emph{Child Controls} and \emph{Family Controls}." ///
			"We test for the equality of coefficients in columns (1) and (2), (3) and (4), (5) and (6), as well as (7) and (8), respectively." ///
			"Next to point estimates, we present standard errors (in parentheses), and 95\% confidence intervals (in brackets)."
#delimit;
textab, title(`title') name(`name') replace
width(1.3) file(tex) notesize(`notesize')
note_paper("`source' `desc' `controls' `controlfct' `standard' `signif' `error'")
data(`data')
inpath(${tablepath}) path_paper(${paper_tables});
#delimit cr

// -----------------------------------------------------
// Table S.6
// -----------------------------------------------------
use "${data}data_estim.dta", replace

local name tabS6

/*Get Estimates*/
local models ///
ts6_1 ts6_2 ts6_3 ts6_4
local coefs ///
pgs_edu f_over1 c.pgs_edu#c.f_over1

output `models', clist(`coefs') replay

/*Set up Table*/
texdoc init "${tablepath}`name'.tex", replace force
#delimit;
tex \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi};
tex \begin{tabular*}{1\linewidth}{@{\hskip\tabcolsep\extracolsep\fill}l*{4}{c}};
tex \toprule;
tex	\thead[l]{{Outcome:}\\{Years of Education}}&
	\thead{(1)}&
	\thead{(2)}&
	\thead{(3)}&
	\thead{(4)}\\;
tex \midrule;
tex \thead[l]{\PGS}						&${c_1_1}&${c_2_1}&${c_3_1}&${c_4_1}\\;
tex \thead[l]{\IQuali}					&${c_1_2}&${c_2_2}&${c_3_2}&${c_4_2}\\;
tex \thead[l]{\PGS $\times$ \IQuali}	&${c_1_3}&${c_2_3}&${c_3_3}&${c_4_3}\\;
tex \midrule;
tex \thead[l]{Child Controls}	&\no&\ye&\ye&\ye\\;
tex \thead[l]{Family Controls}	&\no&\no&\ye&\ye\\;
tex \thead[l]{Control Function}	&\no&\no&\no&\ye\\;
tex \thead[l]{N}						&$ob_1&$ob_2&$ob_3&$ob_4\\;
tex \thead[l]{$\text{R}^2$}				&$rs_1&$rs_2&$rs_3&$rs_4\\;
tex \bottomrule;
tex \end{tabular*};
#delimit cr
texdoc close

local title "Testing for Influence of Control Variables"
local desc  "This table shows the joint association of \PGS and \IQuali" ///
			"with completed years of education while sequentially strengthening the set of control variables."
#delimit;
textab, title(`title') name(`name') replace
width(1.3) file(tex) notesize(`notesize')
note_paper("`source' `desc' `controls' `controlfct' `standard' `signif' `error'")
data(`data')
inpath(${tablepath}) path_paper(${paper_tables});
#delimit cr


// -----------------------------------------------------
// Table S.7
// -----------------------------------------------------
use "${data}data_estim.dta", replace
local name tabS7

/*Get Estimates*/
local models t3_1 ts7_1 ts7_2 ts7_3
local coefs pgs_edu ///
f_over1 c.pgs_edu#c.f_over1 ///
f_extend1 c.pgs_edu#c.f_extend1  ///
f_extend2 c.pgs_edu#c.f_extend2 ///
f_extend3 c.pgs_edu#c.f_extend3 

output `models', clist(`coefs') replay

/*Set up Table*/
texdoc init "${tablepath}`name'.tex", replace force
#delimit;
tex \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi};
tex \begin{tabular*}{1\linewidth}{@{\hskip\tabcolsep\extracolsep\fill}l*{4}{c}};
tex \toprule;
tex \multicolumn{1}{c}{\textbf{~}}&
	\multicolumn{1}{c}{\thead[c]{Baseline}}&
	\multicolumn{3}{c}{\thead[c]{Expanded School Quality Indicators}}\\;
tex \cmidrule(lr){2-2}\cmidrule(lr){3-5};
tex \thead[l]{{Outcome:}\\{Years of Education}}
			 &\thead{{~}\\{(1)}}
			 &\thead{{+School Policies}\\{(2)}}
			 &\thead{{+Private School}\\{(3)}}
 			 &\thead{{+Teacher Composition}\\{(4)}}\\;
tex \midrule;
tex \thead[l]{\PGS}						  &${c_1_1}&${c_2_1}  &${c_3_1}&${c_4_1} \\;
tex \thead[l]{\IQuali}					  &${c_1_2}&${c_2_4}  &${c_3_6}&${c_4_8} \\;
tex \thead[l]{\PGS $\times$ \IQuali}	  &${c_1_3}&${c_2_5}  &${c_3_7}&${c_4_9} \\;
tex \midrule;
tex \thead[l]{Child Controls}			&\ye&\ye&\ye&\ye\\;
tex \thead[l]{Family Controls}			&\ye&\ye&\ye&\ye\\;
tex \thead[l]{Control Function}			&\ye&\ye&\ye&\ye\\;
tex \thead[l]{N}						&$ob_1&$ob_2&$ob_3&$ob_4\\;
tex \thead[l]{$\text{R}^2$}				&$rs_1&$rs_2&$rs_3&$rs_4\\;
tex \thead[l]{Outcome Mean}				&$mu_1&$mu_2&$mu_3&$mu_4\\;
tex \thead[l]{Outcome SD}				&$sd_1&$sd_2&$sd_3&$sd_4\\;
tex \bottomrule;
tex \end{tabular*};
#delimit cr
texdoc close

local title "Robustness to Using Expanded School Quality Indicators"
local desc  "This table shows the joint association of \PGS and \IQuali" ///
			"with completed years of education when using expanded versions of the school quality indicators." ///
			"\emph{School policies}: Retention, ability grouping, and school sanctions." ///
			"\emph{Private School}: Private school indicator." ///
			"\emph{Teacher Composition}: White teacher, female teacher."
#delimit;
textab, title(`title') name(`name') replace
width(1.3) file(tex) notesize(`notesize')
note_paper("`source' `desc' `controls' `controlfct' `standard' `signif' `error'")
data(`data')
inpath(${tablepath}) path_paper(${paper_tables});
#delimit cr


// -----------------------------------------------------
// Table S.8
// -----------------------------------------------------
use "${data}data_estim.dta", replace
local name tabS8

/*Get Estimates*/
local models ///
t3_1 ts8_1 ts8_2 ts8_3 ts8_4 ts8_5 ts8_6
local coefs ///
pgs_edu f_over1 c.pgs_edu#c.f_over1 ///
c.RobGeneOther c.RobGeneOther#c.f_over1
output `models', clist(`coefs') replay

/*Set up Table*/
texdoc init "${tablepath}`name'.tex", replace force
#delimit;
tex \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi};
tex \begin{tabular*}{1\linewidth}{@{\hskip\tabcolsep\extracolsep\fill}l*{7}{c}};
tex \toprule;
tex \multicolumn{1}{c}{~}&\multicolumn{1}{c}{\thead{Baseline}}&\multicolumn{6}{c}{\thead{{+ Controls for}\\{Other Polygenic Indexes}}}\\;
tex \cmidrule(lr){2-2}\cmidrule(lr){3-8};
tex \thead[l]{{~}\\{Outcome:}\\{Years of Education}}&
	\thead{{~}\\{~}\\{(1)}}&
	\thead{{Body Mass}\\{Index}\\{(2)}}&
	\thead{{ADHD}\\{~}\\{(3)}}&
	\thead{{Depressive}\\{Symptoms}\\{(4)}}&
	\thead{{Intelligence}\\{~}\\{(5)}}&
	\thead{{Ever}\\{Smoker}\\{(6)}}&
	\thead{{Sleep}\\{Duration}\\{(7)}}\\;
tex \midrule;
tex \thead[l]{\PGS}							&${c_1_1}&${c_2_1}&${c_3_1}&${c_4_1}&${c_5_1}&${c_6_1}&${c_7_1}\\;
tex \thead[l]{\IQuali}						&${c_1_2}&${c_2_2}&${c_3_2}&${c_4_2}&${c_5_2}&${c_6_2}&${c_7_2}\\;
tex \thead[l]{\PGS $\times$ \IQuali}		&${c_1_3}&${c_2_3}&${c_3_3}&${c_4_3}&${c_5_3}&${c_6_3}&${c_7_3}\\;
tex \thead[l]{Other PGI}					&${c_1_4}&${c_2_4}&${c_3_4}&${c_4_4}&${c_5_4}&${c_6_4}&${c_7_4}\\;
tex \thead[l]{Other PGI $\times$ \IQuali}	&${c_1_5}&${c_2_5}&${c_3_5}&${c_4_5}&${c_5_5}&${c_6_5}&${c_7_5}\\;
tex \midrule;
tex \thead[l]{Child Controls}	&\ye&\ye&\ye&\ye&\ye&\ye&\ye\\;
tex \thead[l]{Family Controls}	&\ye&\ye&\ye&\ye&\ye&\ye&\ye\\;
tex \thead[l]{Control Function}	&\ye&\ye&\ye&\ye&\ye&\ye&\ye\\;
tex \thead[l]{N}				&$ob_1&$ob_2&$ob_3&$ob_4&$ob_5&$ob_6&$ob_7\\;
tex \thead[l]{$\text{R}^2$}		&$rs_1&$rs_2&$rs_3&$rs_4&$rs_5&$rs_6&$rs_7\\;
tex \bottomrule;
tex \end{tabular*};
#delimit cr
texdoc close

local title "Robustness to Including Alternative Polygenic Indexes"
local desc  "This table shows the joint association of \PGS and \IQuali" ///
			"with completed years of education when controlling for additional polygenic indexes." ///
			"We control for other PGIs and their interaction with \IQuali\negspace." ///
			"The relevant PGIs are indicated in the column header."
#delimit;
textab, title(`title') name(`name') replace
width(1.3) file(tex) notesize(`notesize')
note_paper("`source' `desc' `controls' `controlfct' `standard' `signif' `error'")
data(`data')
inpath(${tablepath}) path_paper(${paper_tables});
#delimit cr


// -----------------------------------------------------
// Table S.9
// -----------------------------------------------------
tempfile tempsave
import delimited "${results}out_addhealth.res", varnames(10) rowrange(10) clear
drop corrected*
rename (uncorrected_*) (*)
gen group="base"
gen str10 percent=string(abs(coef[_n]/coef[_n-2]*100), "%9.0f")
replace percent=percent+"\%" if percent!="."
replace percent="" if percent=="."
save "`tempsave'", replace

foreach x in addhealth hrs wls ukb{
	import delimited "${results}out_`x'.res", varnames(10) rowrange(10) clear
	drop uncorrected*
	rename (corrected_*) (*)
	gen group="`x'"
	gen str10 percent=string(abs(coef[_n]/coef[_n-2]*100), "%9.0f")
	replace percent=percent+"\%" if percent!="."
	replace percent="" if percent=="."
	append using  "`tempsave'"
	save "`tempsave'", replace
}

use "`tempsave'", clear
gen p=2*normal(-abs(coef/se))


replace variable_name= "\PGS" if variable_name=="pgs_edu"
replace variable_name= "\IQuali" if variable_name=="f_over1"
replace variable_name= "\PGS $\times$ \IQuali" if variable_name=="f_over1_int"

/*Construct Table*/
local name tabS9

sumstat variable_name coef se p percent, name(${tablepath}`name') replace ///
flush_l(variable_name) flush_c(coef se p percent) format_3(coef se p) ///
headlabel("~" "Coefficient" "Standard~Error" "p-value" "Substitutability") ///
grouporder(base addhealth hrs wls ukb) ///
grouplabel("Baseline" "Add~Health~($\rho=1.968$)" "Health~and~Retirement~Study~($\rho=1.413$)" "Wisconsin~Longitudinal~Study~($\rho=1.649$)" "UK~Biobank~($\rho=1.452$)")	///
first(variable_name) long(no) landscape(no) width(1) grouping(yes)

local title "Robustness to Using Measurement-error-corrected PGI \citep{Becker2021}"
local desc  "This table shows the joint association of \PGS and \IQuali" ///
			"with completed years of education." ///
			"We apply the correction method of \citet{Becker2021} to account for measurement error in \PGS\negspace." ///
			"The correction method is based on $ \rho=h^2_{SNP}/R^2 $, where $ h^2_{SNP} $ indicates SNP heritability and" ///
			"$ R^2 $ the share of variation in educational attainment explained by \PGS\negspace. For Add Health, we take $ \rho $" ///
			"from \citet{SanzdeGaldeano2019}, for all other data sets we take $ \rho $ from \citet{Becker2021}." ///
			"Standard errors are bootstrapped with 1,000 draws." ///
			"These standard errors are likely conservative \citep{Becker2021,SanzdeGaldeano2019}."
textab,	title(`title') name(`name') replace ///
width(1) file(tex) notesize(`notesize')  ///
note_paper("`source' `desc'")  ///
data("`data'") ///
inpath(${tablepath}) path_paper(${paper_tables})

// -----------------------------------------------------
// Table S.10
// -----------------------------------------------------
use "${data}data_estim.dta", replace
local name tabS10

/*Get Estimates*/
local models ///
ts10_1 ts10_2 ts10_3 ts10_4 ts10_5 ts10_6 ts10_7 ts10_8
local coefs ///
pgs_edu f_over1 c.pgs_edu#c.f_over1

output `models', clist(`coefs') replay

/*Set up Table*/
texdoc init "${tablepath}`name'.tex", replace force
#delimit;
tex \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi};
tex \begin{tabular*}{1\linewidth}{@{\hskip\tabcolsep\extracolsep\fill}l*{8}{c}};
tex \toprule;
tex \multicolumn{1}{c}{~}&\multicolumn{4}{c}{\thead{Wave 1/2}}&\multicolumn{4}{c}{\thead{Wave 3/4}}\\;
tex \cmidrule(lr){2-5}\cmidrule(lr){6-9};
tex	\thead[l]{{}\\{}}&
	\thead{{PVT}\\{(1)}}&
	\thead{{Risk}\\{(2)}}&
	\thead{{Patience}\\{(3)}}&
	\thead{{Subj. health}\\{(4)}}&
	\thead{{PVT}\\{(5)}}&
	\thead{{Risk}\\{(6)}}&
	\thead{{Patience}\\{(7)}}&
	\thead{{Subj. health}\\{(8)}}\\;
tex \midrule;
tex \thead[l]{\PGS}						&${c_1_1}&${c_2_1}&${c_3_1}&${c_4_1}&${c_5_1}&${c_6_1}&${c_7_1}&${c_8_1}\\;
tex \thead[l]{\IQuali}					&${c_1_2}&${c_2_2}&${c_3_2}&${c_4_2}&${c_5_2}&${c_6_2}&${c_7_2}&${c_8_2}\\;
tex \thead[l]{\PGS $\times$ \IQuali}	&${c_1_3}&${c_2_3}&${c_3_3}&${c_4_3}&${c_5_3}&${c_6_3}&${c_7_3}&${c_8_3}\\;
tex \midrule;
tex \thead[l]{Child Controls}			&\ye&\ye&\ye&\ye&\ye&\ye&\ye&\ye\\;
tex \thead[l]{Family Controls}			&\ye&\ye&\ye&\ye&\ye&\ye&\ye&\ye\\;
tex \thead[l]{Control Function}			&\ye&\ye&\ye&\ye&\ye&\ye&\ye&\ye\\;
tex \thead[l]{N}						&$ob_1&$ob_2&$ob_3&$ob_4&$ob_5&$ob_6&$ob_7&$ob_8\\;
tex \thead[l]{$\text{R}^2$}				&$rs_1&$rs_2&$rs_3&$rs_4&$rs_5&$rs_6&$rs_7&$rs_8\\;
tex \bottomrule;
tex \end{tabular*};
#delimit cr
texdoc close

local title "Association of Skill Measures with \PGS and \IQuali by Waves"
local desc 	"This table shows the joint association of \PGS and \IQuali" ///
			"with cognitive skills, preferences, and health." ///
			"Columns (1)-(4) show skill measures in waves 1 and 2 of Add Health." ///
			"Columns (5)-(8) show skill measures in waves 3 and 4 of Add Health." ///
			"We restrict the sample to all individuals in grade 9 and higher during wave 1 of Add Health" ///
			"with non-missing information on the relevant skill measures."
#delimit;
textab, title(`title') name(`name') replace
width(1.3) file(tex) notesize(`notesize')
note_paper("`source' `desc' `controls' `standard' `signif' `error'")
data(`data')
inpath(${tablepath}) path_paper(${paper_tables});
#delimit cr


// -----------------------------------------------------
// Table S.11
// -----------------------------------------------------
local groups 		education health cog preferences personality parental
local education 	eduyears hs coll2y coll4y postgrad
local health 		health_sub health_obj
local cog 			pvt
local preferences 	risk pat
local personality 	intel consc extra agree neuro
local parental		inv auth2

use `education' `health' `cog' `preferences' `personality' `parental' sample1 w0 if sample1==1 using "${data}data_estim.dta", clear

la var eduyears "Years of Education"
la var hs "High School Degree"
la var coll2y "2-year College Degree"
la var coll4y "4-year College Degree"
la var postgrad "Post-Graduate Degree"
la var health_sub "Subjective"
la var health_obj "Objective"
la var pvt "Picture Vocabulary Test"
la var intel "Openness"
la var consc "Conscientousness"
la var extra "Extraversion"
la var agree "Agreeableness"
la var neuro "Neuroticism"
la var risk "Risk Aversion"
la var pat "Patience"
la var inv "Parental Time Investment"
la var auth2 "Parenting Style"

foreach g of local groups{
	gstats tabstat ``g'', s(count mean sd min max) prettystats matasave
	mata: `g' = (GstatsOutput.getOutputGroup(1))
	mata: st_matrix("`g'", `g')
	matname `g' ``g'', rows(.)
	xsvmat  `g', names(col) rowlabels(v1) idstr("`g'") nidstr(group) saving("${temp}sum_`g'", replace) rename(c1 count`s' c2 mean`s' c3 sd`s' c4 min`s' c5 max`s')
}
clear
foreach g of local groups{
	 append using "${temp}sum_`g'.dta"
}
replace mean=mean+0.000001

/*Construct Table*/
local name tabS11

#delimit;
sumstat v1 count mean sd min max, name(${tablepath}`name') replace
flush_l(v1) flush_c(count mean sd min max) format_0(count) format_2(mean sd min max)
headlabel("~" "Obs." "Mean" "SD" "Min" "Max")
first(v1) long(no) landscape(no) width(1)
grouping(yes) grouplabel("Educational~Attainment" "Health" "Cognitive~Skills" "Preferences" "Personality" "Family~Investments") ///
grouporder(education health cog preferences personality parental);
#delimit cr

local title "Summary Statistics (Outcomes)"
local desc 	"This table shows summary statistics for outcome variables in our core analysis sample." ///
			"The sample is restricted to genotyped individuals who (i) are of European descent," ///
			"and (ii) attended an Add Health high school or an associated feeder school in wave 1."
#delimit;
textab,	title(`title') name(`name') replace width(1) file(tex) notesize(`notesize')
		note_paper("`source' `desc'") data(`data')
        inpath(${tablepath}) path_paper(${paper_tables});
#delimit cr


// -----------------------------------------------------
// Table S.12
// -----------------------------------------------------
local groups 	pgs sclchar ses
local pgs 		pgs_edu
local sclchar 	f_over1 f_over2 teach_master teach_5years teach_new class
local ses 		sesbelsky

use `pgs' `sclchar' `ses' sample1 w0 if sample1==1 using "${data}data_estim.dta", clear

la var pgs_edu "\PGS"
la var f_over1 "\IQuali (PCA)"
la var f_over2 "\IQuali \citep{Anderson2008}"
la var class "Class Size"
la var teach_5years "Experienced Teacher (\%)"
la var teach_new "New Teacher (\%)"
la var teach_master "Teacher w/ MA (\%)"
la var sesbelsky "Social Origins Factor Score"

foreach g of local groups{
	gstats tabstat ``g'', s(count mean sd min max) prettystats matasave
	mata: `g' = (GstatsOutput.getOutputGroup(1))
	mata: st_matrix("`g'", `g')
	matname `g' ``g'', rows(.)
	xsvmat  `g', names(col) rowlabels(v1) idstr("`g'") nidstr(group) saving("${temp}sum_`g'", replace) rename(c1 count`s' c2 mean`s' c3 sd`s' c4 min`s' c5 max`s')
}
clear
foreach g of local groups{
	 append using "${temp}sum_`g'.dta"
}
replace mean=mean+0.000001

/*Construct Table*/
local name tabS12

#delimit;
sumstat v1 count mean sd min max, name(${tablepath}`name') replace
flush_l(v1) flush_c(count mean sd min max) format_0(count) format_2(mean sd min max)
headlabel("~" "Obs." "Mean" "SD" "Min" "Max")
first(v1) long(no) landscape(no) width(1)
grouping(yes) grouplabel("Polygenic~Scores" "School~Characteristics" "Family~SES") grouporder(pgs sclchar ses);
#delimit cr

local title "Summary Statistics (Variables of Interest)"
local desc 	"This table shows summary statistics for variables of interest in our core analysis sample." ///
			"The sample is restricted to genotyped individuals who (i) are of European descent," ///
			"and (ii) attended an Add Health high school or an associated feeder school in wave 1."
#delimit;
textab,	title(`title') name(`name') replace width(1) file(tex) notesize(`notesize')
		note_paper("`source' `desc'") data(`data')
        inpath(${tablepath}) path_paper(${paper_tables});
#delimit cr

// -----------------------------------------------------
// Table S.13
// -----------------------------------------------------
local groups 		child parents cfctio school pgs
local child 		gender age_home_w1 firstborn
local parents 		edumother edufather agemum rel ///
					bornus_father bornus_mother ///
					bartik_mean_mother bartik_mean_father ///
					bartik_sd_mother bartik_sd_father
local cfctio 		peer_white peer_single peer_mum_educ peer_sex  ///
					peer_mig
local school 		f_stri track retent teach_white teach_female private
local pgs 			pgs_bmi pgs_adh pgs_dep pgs_int pgs_smk pgs_slp

use `child' `parents' `school' `cfctio' `pgs' sample1 w0 if sample1==1 using "${data}data_estim.dta", clear

la var gender "Female"
la var age_home_w1 "Age in Months (Wave 1)"
la var firstborn "Firstborn"
la var edumother "Education Mother (in Years)"
la var edufather "Education Father (in Years)"
la var bornus_mother "Foreign-born Mother"
la var bornus_father "Foreign-born Father"
la var bartik_mean_mother "Potential Wage/Hour Mother (Mean)"
la var bartik_mean_father "Potential Wage/Hour Father (Mean)"
la var bartik_sd_mother "Potential Wage/Hour Mother (SD)"
la var bartik_sd_father "Potential Wage/Hour Father (SD)"
la var agemum "Maternal Age at Birth"
la var rel "Christian"
la var peer_mum_educ "Educ. Mother (School Average)"
la var peer_single "Single Parents (School Share)"
la var peer_white "White Student (School Share)"
la var peer_mig "Migration Background (School Share)"
la var peer_sex "Female (School Share)"
la var track "Ability Groups"
la var retent "Retention Policy (\%)"
la var f_stri "Strictness Index"
la var private "Private School"
la var teach_white "White Teacher (\%)"
la var teach_female "Female Teacher (\%)"
la var pgs_bmi "BMI"
la var pgs_adh "ADHD"
la var pgs_dep "Depressive Symptoms"
la var pgs_int "Intelligence"
la var pgs_smk "Ever Smoker"
la var pgs_slp "Sleep Duration"


foreach g of local groups{
	gstats tabstat ``g'', s(count mean sd min max) prettystats matasave
	mata: `g' = (GstatsOutput.getOutputGroup(1))
	mata: st_matrix("`g'", `g')
	matname `g' ``g'', rows(.)
	xsvmat  `g', names(col) rowlabels(v1) idstr("`g'") nidstr(group) saving("${temp}sum_`g'", replace) rename(c1 count`s' c2 mean`s' c3 sd`s' c4 min`s' c5 max`s')
}
clear
foreach g of local groups{
	 append using "${temp}sum_`g'.dta"
}
replace mean=mean+0.000001

/*Construct Table*/
local name tabS13

#delimit;
sumstat v1 count mean sd min max, name(${tablepath}`name') replace
flush_l(v1) flush_c(count mean sd min max) format_0(count) format_2(mean sd min max)
headlabel("~" "Obs." "Mean" "SD" "Min" "Max")
first(v1) long(no) landscape(no) width(1)
grouping(yes) grouplabel("Child~Controls" "Family~Controls" "Control~Function" "Other~School~Characteristics" "Polygenic~Scores") grouporder(child parents cfctio school pgs endog);
#delimit cr

local title "Summary Statistics (Controls)"
local desc 	"This table shows summary statistics for control variables in our core analysis sample." ///
			"The sample is restricted to genotyped individuals who (i) are of European descent," ///
			"and (ii) attended an Add Health high school or an associated feeder school in wave 1."
#delimit;
textab,	title(`title') name(`name') replace width(1) file(tex) notesize(`notesize')
		note_paper("`source' `desc'") data(`data')
        inpath(${tablepath}) path_paper(${paper_tables});
#delimit cr
